import React from 'react';
import {injectIntl, language} from './intl';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import TextInput from 'Shared/components/TextInput';
import {PERMISSION} from './constants';
import {FORM_OPTIONS, FORM_ROW_OPTIONS} from '../constants';
import WrappedSelect from '../common/WrappedSelect';
import {hasPermissions} from './utils';
import ImportPanel from './ImportPanel';
import KdImportPanel from './KdImportPanel';
import TagSelect from 'Shared/components/TagSelect';
import DateRangePicker from 'Shared/components/DateRangePicker';
import TextArea from 'Shared/components/TextArea';
import {packingListType, packingBusinessType, dealerType} from '../Enum';
const ButtonGroup = Button.Group;
const FormItem = Form.Item;
const EMPTY_ARRAY = [];
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
const dealerConditions = {
    type: dealerType.总代
};
/*eslint-disable  react/prefer-stateless-function*/
class QueryPanel extends React.PureComponent {
    state = {
        loading: false,
        showImportPanel: false,
        showKdImportPanel: false
    }
    handleFilter = (value, name) => {
        this.props.onConditionsChange(name, value);
    };
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.onClickQuery();
    };

    onClickQuery = () => {
        this.props.querySearch();
    }
    onChangeImport = () => {
        this.setState({
            showImportPanel: true
        });
    }
    onChangeKdImport = () => {
        this.setState({
            showKdImportPanel: true
        });
    }
    onCloseImportPanel = () => {
        this.setState({
            showImportPanel: false
        });
    }
    onImportChange = data => {
        if(data)
            this.setState({
                showImportPanel: false
            });
    }
    onCloseKdImportPanel = () => {
        this.setState({
            showKdImportPanel: false
        });
    }
    onKdImportChange = data => {
        if(data)
            this.setState({
                showKdImportPanel: false
            });
    }
    render() {
        const otherBtns = [];
        if(hasPermissions(this.props.permissions, PERMISSION.import))
            otherBtns.push(<Button key="import" onClick={this.onChangeImport}>{this.props.intl.formatMessage({
                id: 'queryPanel.btn.import',
                defaultMessage: '导入'
            })}</Button>);
        if(hasPermissions(this.props.permissions, PERMISSION.KdImport))
            otherBtns.push(<Button key="KdImport" onClick={this.onChangeKdImport}>{this.props.intl.formatMessage({
                id: 'queryPanel.btn.KdImport',
                defaultMessage: 'KD索赔箱单导入'
            })}</Button>);
        const dealerInfo = this.props.conditions.dealerId ? {
            id: this.props.conditions.dealerId,
            code: this.props.conditions.dealerCode,
            name: this.props.conditions.dealerName
        } : null;
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.dealerId',
                                    defaultMessage: '总代'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DealerSelectPanel
                                    conditions={dealerConditions}
                                    value={dealerInfo}
                                    onSelect={this.props.onSelect} />
                            </FormItem>
                        </Col>
                        {this.props.brands.length > 1 ? <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.brandId',
                                    defaultMessage: '品牌'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    name="brandId"
                                    allowClear
                                    value={this.props.conditions.brandId}
                                    options={this.props.brands}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col> : null}
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.code',
                                    defaultMessage: '包装箱单号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="code"
                                    value={this.props.conditions.code}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilter}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.saleConfirmationrCode',
                                    defaultMessage: '销售确认书编号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="saleConfirmationrCode"
                                    value={this.props.conditions.saleConfirmationrCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.batchNumber',
                                    defaultMessage: '批次号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="batchNumber"
                                    value={this.props.conditions.batchNumber}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.transBatchNumber',
                                    defaultMessage: '发运批次号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="transBatchNumber"
                                    value={this.props.conditions.transBatchNumber}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.kdClaim',
                                    defaultMessage: 'KD索赔单号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="kdClaim"
                                    value={this.props.conditions.kdClaim}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.partCode',
                                    defaultMessage: '备件编号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextArea
                                    name="partCode"
                                    value={this.props.conditions.partCode}
                                    placeholder={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.placeholder',
                                        defaultMessage: '多个输入，以回车隔开'
                                    })}
                                    onBlur={this.handleFilter}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.businessType',
                                    defaultMessage: '业务类型'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    name="businessType"
                                    value={this.props.conditions.businessType}
                                    options={packingBusinessType.toList(language)}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.createTime',
                                    defaultMessage: '创建时间'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DateRangePicker name="createTime" value={this.props.conditions.createTime}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.status',
                                    defaultMessage: '状态'
                                })}
                                {...FORM_ROW_OPTIONS.item}>
                                <TagSelect
                                    name="status"
                                    value={this.props.conditions.status}
                                    options={packingListType.toList(language)}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="operation-buttons">
                        <Col span={6}>
                            <Button
                                key="query"
                                type="primary"
                                onClick={this.onClickQuery}
                                loading={this.props.loading}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}
                            </Button>
                            <Button key="clear" onClick={this.props.onClickReset}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}
                            </Button>
                        </Col>
                        <Col span={18} className="col-align-right">
                            <ButtonGroup>{otherBtns}</ButtonGroup>
                        </Col>
                    </Row>
                    {
                        this.state.showImportPanel &&
                        <ImportPanel
                            selectKey={'1'}
                            onCancel={this.onCloseImportPanel}
                            onImportChange={this.onImportChange} />
                    }
                    {
                        this.state.showKdImportPanel &&
                        <KdImportPanel
                            selectKey={'1'}
                            onCancel={this.onCloseKdImportPanel}
                            onImportChange={this.onKdImportChange} />
                    }
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    brands: PropTypes.array.isRequired,
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickReset: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onConditionsPressEnter: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    intl: PropTypes.object,
};
import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel, selectDealer} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);


const getBrandsData = createSelector(
    state => state.getIn(['page', 'domainData', 'brands', 'data']),
    initData => {
        const data = initData.toJS() || EMPTY_ARRAY;
        return data.map(item => ({
            text: item.name,
            value: item.id
        }));
    }
);

const mapStateToProps = state => ({
    conditions: getConditions(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    permissions: getPermissions(state),
    brands: getBrandsData(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    onConditionsPressEnter: (value, name) => {
        dispatch(saveQueryCondition(name, value));
        dispatch(querySearch());
    },
    querySearch: () => dispatch(querySearch()),
    onClickReset: () => dispatch(resetQueryPanel()),
    onSelect: data => dispatch(selectDealer(data))
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(QueryPanel));
